package com.qyhl.webtv.module_user.login.wx;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.dialog.LoadingDialog;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.login.NewLoginActivity;
import com.qyhl.webtv.module_user.util.LoginUtils;

import java.util.Map;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * 微信登陆界面
 *
 * @author helin
 * @date 2019年3月18日10:55:06
 */
public class WXLoginFragment extends BaseFragment implements WXLoginContract.WXLoginView {
    private View view;
    private WXLoginContract.WXLoginPresenter presenter;
    /**
     * 微信登陆按钮
     */
    @BindView(R2.id.btn_wx_login)
    Button btn_wx_login;
    private LoadingDialog.Builder builder;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.login_fragment_wxlogin, container, false);

        return view;
    }

    @Override
    public void initImmersionBar() {

    }


    @Override
    protected void create() {
        init();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    private void init() {
        presenter = new WXLoginPresenter(this);
        btn_wx_login.setOnClickListener(v -> {
            if(getActivity() instanceof NewLoginActivity&& ((NewLoginActivity) getActivity()).isAgreeRule()){
                builder.show();
                presenter.wxLogin(getActivity());
            }else{
                showToast("请认真阅读并同意《使用条款》和《隐私政策》");
            }

        });

        builder = new LoadingDialog.Builder(getActivity());
        builder.setTitle("登录中...");
        builder.setCanceledOnTouchOutside(false);
        builder.setCancelable(true);
    }

    @Override
    public void wxLoginError(String msg) {
        builder.dismiss();
        Toasty.normal(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 微信注册绑定
     *
     * @param map
     * @param openid
     */
    @Override
    public void wxRegister(Map<String, String> map, String openid) {
        builder.dismiss();

        ARouter.getInstance().build(ARouterPathConstant.BINDING_ACTIVITY)
                .withString("uid", map.get("uid"))
                .withString("name", map.get("name"))
                .withString("gender", map.get("gender"))
                .withString("iconurl", map.get("iconurl"))
                .navigation(this.getActivity(), 666);

    }

    @Override
    public void loginSuccess(UserInfoBean bean) {
        builder.dismiss();
        Toasty.normal(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
        LoginUtils.setloginSuccess(bean, ActionConstant.ACCOUNTLOGIN);
        BusFactory.getBus().post(new Event.finishActivity());
    }

    @Override
    public void onResume() {
        if (builder != null) {
            builder.dismiss();
        }
        super.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 666 && resultCode == 666) {
            BusFactory.getBus().post(new Event.finishActivity());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null && view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        view = null;
    }
}
